import modelExtend from 'dva-model-extend'
import {config} from 'utils'
import * as linkService from './service'
import {pageModel} from 'utils/model'

const {
  findAllLink,
  insertLinkPub,
  findAllPubLink,
  removeLinkPub,
  removeLinks,
  removeLink,
  updateLink,
  insertLink,
  getPubTarget,
  getPgPletIdLink,
} = linkService
const {prefix} = config

export default modelExtend(pageModel, {
  namespace: 'contentLink',
  state: {
    selectedRowKeysPub: [],
    // editContentId: 'editor0', // TODO id
    // editInitialContent: '', // 初始内容
    // editHtmlContent: '', // html 格式的内容
    isPubModal: false, // 发布modal
    modalPubTreeData: [],
    modalPubTreeDataPgPletId: [],
    tabPaneKey: '1', // tab key，默认第一个 tab 被选中

    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
    // 链接发布查询时默认按单位查询
    // switchLinkPubFilterType: 'region',
  },
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen((location) => {
        if (location.pathname === '/content/link') {
          const payload = location.query || {page: 1, pageSize: 10}
          dispatch({
            type: 'query',
            payload,
          })
        }
      })
    },
  },

  effects: {
    * query ({payload = {}}, {call, put}) {
      const data = yield call(findAllLink, payload)
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.data,
            selectedRowKeys: [],
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: data.total,
            },
          },
        })
      }
    },

    * delete ({payload}, {call, put, select}) {
      const data = yield call(removeLink, payload)
      const {selectedRowKeys} = yield select(_ => _.contentLink)
      if (data.success) {
        yield put({type: 'updateState', payload: {selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload)}})
      } else {
        throw data
      }
    },

    * multiDelete ({payload}, {call, put}) {
      console.log(payload.ids.length)
      const data = yield call(removeLinks, payload)

      if (data.success) {
        yield put({type: 'updateState', payload: {selectedRowKeys: []}})
      } else {
        throw data
      }
    },

    * create ({payload}, {call, put}) {
      const data = yield call(insertLink, payload)
      if (data.success) {
        console.log("create success")
        yield put({type: 'hideModal'})
      } else {
        throw data
      }
    },

    // TODO 发布table删除
    * deletePub ({payload}, {call, put, select}) {
      const data = yield call(removeLinkPub, payload)
      const {selectedRowKeysPub} = yield select(_ => _.contentLink)
      if (data.success) {
        yield put({
          type: 'updateState', payload: {
            selectedRowKeysPub: selectedRowKeysPub.filter((_, i) => {
              return _ !== payload.pgPletId
            }),
          },
        })
        //  const {list2} = yield select(state => state.contentArticle.list2)
        //  console.log(list2)
      } else {
        throw data
      }
    },

    // TODO 发布链接
    * insertPubLinkModel ({payload}, {select, call, put}) {
      // 加入发布分类判定
      let {toWho} = payload
      const data = yield call(insertLinkPub, payload)
      if (data) {
        yield put({
          type: 'hidePubModal',
          payload: {
            modalPubTreeData: [],
          },
        })
      }
    },
    // 获取发布的链接
    * getLinkPubModel ({payload = {}}, {call, put, select}) {
      const data = yield call(findAllPubLink, payload)
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list2: data.data,
            selectedRowKeysPub: [],
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10, //渲染有问题
              total: data.total,
            },
          },
        })
        // const {linkPubFilterType} = data
        // yield put({
        //   type: 'contentLinkRefresh',
        //   payload: {
        //     switchLinkPubFilterType: linkPubFilterType,
        //   },
        // })
      }
    },
    * update ({payload}, {select, call, put}) {
      // const id = yield select(({user}) => user.currentItem.id)
      // const newUser = {...payload, id}
      // const data = yield call(updateArticle, newUser)
      const data = yield call(updateLink, payload)
      if (data.success) {
        yield put({type: 'hideModal'})
      } else {
        throw data
      }
    },

    // 获取发布单位或角色
    * getPubTargetModel ({payload}, {select, call, put}) {
      const data = yield call(getPubTarget, payload)
      if (data) {
        yield put({
          type: 'showPubModal',
          payload: {
            modalPubTreeData: data.data,
          },
        })
      }
    },
    // 获取发布频道
    * getPgPletId ({payload}, {call, put}) {
      const data = yield call(getPgPletIdLink, payload)
      if (data) {
        yield put({
          type: 'showPubModal',
          payload: {
            modalType: 'publish',
            modalPubTreeDataPgPletId: data.data,
          },
        })
      }
    },

  },

  reducers: {

    showModal (state, {payload}) {
      return {...state, ...payload, modalVisible: true}
    },
    showPubModal (state, {payload}) {
      return {...state, ...payload, PubmodalVisible: true}
    },
    hideModal (state) {
      return {...state, modalVisible: false}
    },
    hidePubModal (state) {
      return {...state, PubmodalVisible: false}
    },
    editorContentUpdate (state, {payload}) {
      return {...state, ...payload}
    },

    contentLinkRefresh ({payload}, state) {
      return {...state, ...payload}
    },

  },
})
